/*
 *     Prionace - A file navigator to manage your files
 *
 *     Copyright (C) 2012 Francivan Bezerra
 *
 *     This file is part of Prionace.
 *
 *     Prionace is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Prionace is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Prionace.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fba.prionace.ui;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Vector;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;

import org.fba.prionace.file.JFile;

public class KeyAction implements ActionListener {

	private FilesPane target;

	public KeyAction() {
	}

	public void initKeys() {
		target.registerKeyboardAction(this, "about",
				KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0),
				JComponent.WHEN_IN_FOCUSED_WINDOW);

		target.registerKeyboardAction(this, "rename",
				KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0),
				JComponent.WHEN_IN_FOCUSED_WINDOW);

		target.registerKeyboardAction(this, "refresh",
				KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0),
				JComponent.WHEN_IN_FOCUSED_WINDOW);

		target.registerKeyboardAction(this, "delete",
				KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0),
				JComponent.WHEN_IN_FOCUSED_WINDOW);

		target.registerKeyboardAction(this, "close tab", KeyStroke
				.getKeyStroke(KeyEvent.VK_W, InputEvent.CTRL_DOWN_MASK),
				JComponent.WHEN_IN_FOCUSED_WINDOW);

		target.registerKeyboardAction(this, "refresh", KeyStroke.getKeyStroke(
				KeyEvent.VK_R, InputEvent.CTRL_DOWN_MASK),
				JComponent.WHEN_IN_FOCUSED_WINDOW);

		target.registerKeyboardAction(this, "invert", KeyStroke.getKeyStroke(
				KeyEvent.VK_I, InputEvent.CTRL_DOWN_MASK),
				JComponent.WHEN_IN_FOCUSED_WINDOW);

		target.registerKeyboardAction(this, "select all", KeyStroke
				.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_DOWN_MASK),
				JComponent.WHEN_IN_FOCUSED_WINDOW);

		target.registerKeyboardAction(this, "select none", KeyStroke
				.getKeyStroke(KeyEvent.VK_A, InputEvent.SHIFT_DOWN_MASK
						+ InputEvent.CTRL_DOWN_MASK),
				JComponent.WHEN_IN_FOCUSED_WINDOW);

		target.registerKeyboardAction(this, "regex select", KeyStroke
				.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK),
				JComponent.WHEN_IN_FOCUSED_WINDOW);

		target.registerKeyboardAction(this, "hide", KeyStroke.getKeyStroke(
				KeyEvent.VK_H, InputEvent.CTRL_DOWN_MASK),
				JComponent.WHEN_IN_FOCUSED_WINDOW);

		target.registerKeyboardAction(this, "location", KeyStroke.getKeyStroke(
				KeyEvent.VK_L, InputEvent.CTRL_DOWN_MASK),
				JComponent.WHEN_IN_FOCUSED_WINDOW);
	}

	public FilesPane getTarget() {
		return target;
	}

	public void setTarget(FilesPane target) {
		this.target = target;
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("about")) {
			AboutDialog about = new AboutDialog();
			JOptionPane.showMessageDialog(target, about, "About Prionace",
					JOptionPane.PLAIN_MESSAGE);
		} else if (e.getActionCommand().equals("rename")) {
			JFile rename = target.getSelectedViewer().getFirstSelected();
			if (rename != null) {
				rename.rename();
			}
		} else if (e.getActionCommand().equals("refresh")) {
			target.getSelectedViewer().refreshFilesList();
		} else if (e.getActionCommand().equals("delete")) {
			Vector<File> files = target.getSelectedViewer().getSelectedFiles();
			if (files.size() > 0) {
				JPanel message;
				JList<String> list;
				JScrollPane listScroller;
				Vector<String> names = new Vector<String>();

				for (File file : files) {
					names.add(file.getName());
				}

				message = new JPanel(new BorderLayout(0, 10));
				list = new JList<String>(names);
				listScroller = new JScrollPane(list);
				listScroller.setPreferredSize(new Dimension(300, 100));

				message.add(new JLabel("Remover:"), BorderLayout.NORTH);
				message.add(listScroller, BorderLayout.CENTER);

				int option = JOptionPane.showConfirmDialog(target, message,
						"Confirmar operação", JOptionPane.YES_NO_OPTION);

				if (option == JOptionPane.YES_OPTION) {
					target.getSelectedViewer().deleteSelected();
				}
			}
		} else if (e.getActionCommand().equals("close tab")) {
			if (target.getTab().getTabCount() > 1) {
				target.getTab().removeTabAt(target.getTab().getSelectedIndex());
			}
		} else if (e.getActionCommand().equals("invert")) {
			target.getSelectedViewer().invertSelection();
		} else if (e.getActionCommand().equals("select all")) {
			target.getSelectedViewer().selectAll();
		} else if (e.getActionCommand().equals("select none")) {
			target.getSelectedViewer().selectNone();
		} else if (e.getActionCommand().equals("regex select")) {
			String regex = JOptionPane.showInputDialog(target,
					"Expressão regular:", "Selecionar arquivos",
					JOptionPane.QUESTION_MESSAGE);

			if (regex != null && !regex.isEmpty()) {
				target.getSelectedViewer().selectRegex(regex);
			}
		} else if (e.getActionCommand().equals("hide")) {
			target.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
			target.getSelectedViewer().setHiddenFilesVisible(
					!target.getSelectedViewer().isHiddenFilesVisible());
			target.getSelectedViewer().refreshFilesList();
			target.setCursor(null);
		} else if (e.getActionCommand().equals("location")) {
			String location = JOptionPane.showInputDialog(target.getPathBar(),
					"Local:", target.getSelectedViewer().getCurrentPath());
			if (location != null && !location.isEmpty()) {
				target.setCurrentPath(location);
				target.refreshBars();
			}
		}
	}
}
